<template>
  <div class="supportCenterIndex">
    <c-title :hide="false" :text="info.plugin_name"></c-title>
    <div class="bg_Top">
      <div class="memberBox" v-if="info.member">
        <div class="image">
          <img :src="info.member.avatar_image" alt="" />
        </div>
        <div class="user">
          <div class="name">{{ info.member.nickname }}</div>
          <div class="Id">
            <div class="iconImg">
              <img src="https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/supportCenter%2F%E7%BB%84%209%402x.png" alt="" />
            </div>
            <div class="txt">{{ info.member.uid }}</div>
          </div>
        </div>
      </div>
    </div>
    <div class="remainingUndrawn">
      <div class="remainingUndrawn_box">
        <div class="left">剩余核销额度</div>
        <div class="right">
          <!-- <span>{{ $i18n.t("money") }}</span> -->
          {{ info.quota }}
        </div>
      </div>
      <div class="nav">
        <div class="left" :class="[active == 0 ? 'isact' : '']" @click="navChange(0)">
          核销记录
          <span :class="[active == 0 ? 'act' : '']"></span>
        </div>
        <div class="right" @click="navChange(1)" :class="[active == 1 ? 'isact' : '']">
          额度记录
          <span :class="[active == 1 ? 'act' : '']"></span>
        </div>
      </div>
    </div>
    <div class="list">
      <template v-if="active == 0">
        <div class="lis" @click="checkTap(item, index)" v-for="(item, index) in list" :key="index" style="padding-top: 0.66rem; padding-bottom: 0.63rem;">
          <div class="dateIs">
            <div class="left">{{ item.days_str }}</div>
            <div class="right">
              <van-icon name="arrow-down" v-if="item.checked" />
              <van-icon name="arrow-up" v-if="!item.checked" />
            </div>
          </div>
          <div class="verification">
            <div class="row">
              核销总数 <span>{{ item.sum_withdraw_quota }}</span>
            </div>
            <div class="row">
              核销次数 <span>{{ item.num }}</span>
            </div>
          </div>
          <template v-if="item.checked">
            <div class="userInfo" v-for="(itemKey, index) in item.log" :key="index">
              <div class="userLis">
                <div class="left" v-if="itemKey.has_one_withdraw_member">
                  <div class="image">
                    <img :src="itemKey.has_one_withdraw_member.avatar_image" alt="" />
                  </div>
                  <div class="userDate">
                    <div class="name">{{ itemKey.has_one_withdraw_member.nickname }}</div>
                    <div class="time">{{ itemKey.created_at }}</div>
                  </div>
                </div>
                <div class="right">{{ itemKey.withdraw_quota }}</div>
              </div>
            </div>
          </template>
          <div class="moreTxt" v-if="item.checked && item.page < item.last_page" @click.stop="moreLogTap(item, index)">加载更多</div>
        </div>
      </template>
      <div class="lis" v-if="active == 1">
        <div class="record" v-for="(item, index) in list" :key="index">
          <div class="left">
            <div class="recordTxt">{{ item.type_name }}</div>
            <div class="recordTitle">变动前{{ item.before_quota }}，变动后{{ item.after_quota }}</div>
          </div>
          <div class="right">{{ item.created_at }}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "utils/mixin";
export default {
  mixins: [scrollMixin],
  data() {
    return {
      active: 0,
      info: [],
      list: [],

      isLoadMore: true, //判断是否要加载更多的标识
      page: 1, //当前的页数
      total_page: 0 //总共有多少页
    };
  },
  activated() {
    this.getUser();
  },
  methods: {
    navChange(key) {
      this.active = key;
      this.init();
      this.getData();
    },
    init() {
      this.list = [];
      this.page = 1;
      this.total_page = 0;
    },
    checkTap(item, index) {
      console.log(item.checked);
      if (item.checked) {
        this.list[index].checked = false;
        this.$set(this.list, index, this.list[index]);
      } else {
        this.getDaysLog(item.days, index);
      }
    },
    getDaysLog(day, index) {
      $http
        .post("plugin.support-center.frontend.write-off-log.get-write-off-log", { days: day }, "加载中")
        .then(response => {
          if (response.result === 1) {
            this.list[index].log = response.data.data;
            this.list[index].page = response.data.current_page;
            this.list[index].last_page = response.data.last_page;
            this.list[index].checked = true;
            this.$set(this.list, index, this.list[index]);
            console.log(this.list[index]);
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getData() {
      let url = "";
      let json = {
        page: this.page
      };
      if (this.active == 0) {
        url = "plugin.support-center.frontend.write-off-log.get-write-off-log-to-days";
      } else {
        url = "plugin.support-center.frontend.write-off-log.get-quota-log";
      }
      $http
        .post(url, json)
        .then(response => {
          if (response.result === 1) {
            this.list = response.data.data;
            this.isLoadMore = true;
            this.total_page = response.data.last_page;
            if (!this.total_page) {
              this.total_page = 0;
            }
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    moreLogTap(item, index) {
      console.log(item);
      if (!item.checked && item.page >= item.last_page) {
        return;
      }
      let page = Number(item.page) + 1;
      $http
        .post("plugin.support-center.frontend.write-off-log.get-write-off-log", { days: item.days, page: page }, "加载中")
        .then(response => {
          if (response.result === 1) {
            this.list[index].log = this.list[index].log.concat(response.data.data);
            this.list[index].page = response.data.current_page;
            this.list[index].last_page = response.data.last_page;
            this.list[index].checked = true;
            this.$set(this.list, index, this.list[index]);
            console.log(this.list[index])
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    getMoreData() {
      let url = "";
      if (this.active == 0) {
        url = "plugin.support-center.frontend.write-off-log.get-write-off-log-to-days";
      } else {
        url = "plugin.support-center.frontend.write-off-log.get-quota-log";
      }
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http.get(url, { page: this.page }, "加载中").then(
          response => {
            this.isLoadMore = true;
            if (response.result === 1) {
              var myData = response.data.data;
              this.list = this.list.concat(myData); //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              return;
            }
          },
          function(response) {
            console.log(response);
            // error callback
          }
        );
      }
    },
    getUser() {
      $http
        .post("plugin.support-center.frontend.write-off-log.get-member-info", {})
        .then(response => {
          if (response.result === 1) {
            console.log(response);
            this.info = response.data;
            this.fun.setWXTitle(response.data.plugin_name)
            this.getData();
          }
        })
        .catch(error => {
          console.log(error);
        });
    }
  }
};
</script>
<style lang="scss" scoped>
.moreTxt {
  color: #999;
  margin-bottom: 1rem;
  text-align: center;
}

.bg_Top {
  height: 7.34rem;
  padding: 0 0.87rem;
  box-sizing: border-box;
  background-image: url("https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/supportCenter%2Fbg_help%402x.png");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  padding-top: 1.25rem;

  .memberBox {
    display: flex;
    align-items: center;

    .user {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: flex-start;
      overflow: hidden;
      box-sizing: border-box;

      .name {
        width: 100%;
        overflow: hidden;
        font-size: 1.13rem;
        font-weight: 600;
        text-align: left;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .Id {
        width: 100%;
        display: flex;
        overflow: hidden;

        .iconImg {
          padding: 0 0.19rem;
          border-radius: 0.94rem;
          background: #f0c5b6;
          margin-right: 0.19rem;

          img {
            width: 0.88rem;
            height: 0.69rem;
          }
        }

        .txt {
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }
      }
    }

    .image {
      width: 3.5rem;
      height: 3.5rem;
      background: #b9b9b9;
      border-radius: 100%;
      margin-right: 0.53rem;

      img {
        width: 100%;
        height: 100%;
        border-radius: 100%;
      }
    }
  }
}

.record {
  background: #fff;
  padding: 0.94rem 0 0.63rem 0;
  margin: 0 0.63rem;
  height: 4.03rem;
  border-bottom: 1px solid #ececec;
  display: flex;
  justify-content: space-between;

  .left {
    flex: 1;
    overflow: hidden;
    text-align: left;

    .recordTxt {
      font-size: 0.94rem;
      font-weight: 600;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .recordTitle {
      font-size: 0.75rem;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  .right {
    flex-shrink: 0;
    font-weight: 400;
    color: #7e7e7e;
    font-size: 0.75rem;
  }
}

.record:last-child {
  border-bottom: none;
}
// .record {
//   display: flex;
//   justify-content: space-between;
//   .left {
//     .recordTxt {
//       font-size: 0.94rem;
//       color: #2c2c2c;
//       font-weight: 600;
//     }
//     .recordTitle {
//       font-size: 0.75rem;
//       color: #2c2c2c;
//     }
//   }
// }
.userInfo {
  // margin: 0 0.75rem;
  background: #fff;
  display: flex;
  flex-direction: column;

  .userLis {
    margin: 0 0.63rem;
    height: 4.13rem;
    border-bottom: 1px solid #ececec;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .left {
      flex-shrink: 0;
      width: 55%;
      display: flex;

      .image {
        width: 2.25rem;
        height: 2.25rem;
        background: #c8c8c8;
        border-radius: 50%;
        margin-right: 0.31rem;

        img {
          width: 100%;
          height: 100%;
          border-radius: 50%;
        }
      }

      .userDate {
        flex: 1;
        display: flex;
        flex-direction: column;
        text-align: left;
        overflow: hidden;
        justify-content: space-between;

        .name {
          font-size: 0.81rem;
          color: #2c2c2c;
          font-weight: 600;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
        }

        .time {
          font-size: 0.75rem;
          font-weight: 400;
          color: #7e7e7e;
        }
      }
    }

    .right {
      flex: 1;
      max-width: 45%;
      height: 2.25rem;
      font-weight: 600;
      font-size: 0.81rem;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      text-align: right;
    }
  }

  .userLis:last-child {
    border-bottom: none;
  }
}

.list {
  margin: 0 0.75rem;
  padding-bottom: 3rem;

  .lis {
    background: #fff;
    border-radius: 0.63rem;
    box-sizing: border-box;
    overflow: hidden;

    .verification {
      margin: 0 0.63rem;
      background: #f7f7f7;
      height: 2.25rem;
      border-radius: 0.63rem;
      padding-left: 0.63rem;
      box-sizing: border-box;
      display: flex;
      align-items: center;

      .row {
        font-size: 0.69rem;
        flex: 1;
        text-align: left;
        color: #7e7e7e;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;

        span {
          font-size: 0.94rem;
          color: #000;
        }
      }
    }

    .dateIs {
      display: flex;
      justify-content: space-between;
      padding: 0 0.81rem;
      margin-bottom: 0.59rem;

      .left {
        font-size: 0.88rem;
        font-weight: 600;
      }

      .right {
        color: #7e7e7e;
      }
    }
  }
}

.remainingUndrawn {
  padding: 0 1rem 0 0.5rem;
  box-sizing: border-box;
  margin-top: -1.65rem;

  .nav {
    height: 3.22rem;
    display: flex;
    align-items: center;
    justify-content: space-around;
    font-size: 0.94rem;
    color: #999;

    .left,
    .right {
      position: relative;
      line-height: 1.13rem;
      font-size: 0.94rem;
      cursor: pointer;
    }

    .isact {
      color: #fe5e56;
    }

    .act {
      position: absolute;
      content: "";
      width: 0.94rem;
      height: 0.19rem;
      left: 1.41rem;
      bottom: -0.3rem;
      background: #fe5e56;
      border-radius: 0.94rem;
    }
  }

  .remainingUndrawn_box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.87rem 0.94rem 0.87rem 0.81rem;
    box-sizing: border-box;
    border-radius: 0.63rem;
    background: #fff;

    .left {
      font-size: 0.75rem;
    }

    .right {
      font-size: 1.13rem;

      span {
        font-size: 0.75rem;
      }
    }
  }
}
</style>
